package com.dmz.resold.apartment.csp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * @author lee
 */
@Slf4j
@Component
public class CspQueryJob {


    private String sktServerIp = "26.28.11.120";
    private int sktServerPort = 17007;


    /**
     * CSP交易查询主方法
     *
     * @throws Exception
     */
    public String cspQuery(HouseBean hb) throws Exception {
        /**
         * 本方法执行内容以及步骤
         *
         * @kuroneko1 创建Scoket链接
         * @kuroneko2 组装要发送的报文
         * @kuroneko3 发送报文，并获取返回的报文信息
         * @kuroneko4 解析返回报文，并入库
         */

        log.info("DMZ->CSP，当前时间;{}", DateUtil.getDateTimeStr());
        /* @kuroneko1 */
        ConnectParam param = showSocketParam();
        ISocketLog iSocketLog = new SocketLog();
        ISocketClient client = new SocketClientNew();
        /* @kuroneko2 */

        //获取请求报文流
        String message = hb.getMessage();

        log.info("-----------------DMZ->CSP 传送指令-------------------------------------");
        //报文：4位的字符串长度+交易码+请求报文内容
        String messageToCSP = new DecimalFormat("0000").format(message.getBytes("GBK").length + 4) + hb.getTranCode() + message;
        log.info(messageToCSP);

        //发送CSP，并接收CSP返回报文result1
        String result1 = client.send(messageToCSP, param, iSocketLog);
        log.info("------------------------[CSP->DMZ]--------------------------------------");

        //此处增加返回报文的判断逻辑，如果为空或者长度小于八，则不进行截取
        if (!"".equals(result1) && result1.length() >= 8) {
            log.info("CSP反馈报文为：{}", result1);

            return result1;
        } else {
            log.error("CSP返回报文内容有错，长度小于8或者为空{}", result1);
        }

        return null;
    }


    //此交易为获取上一次处理的流水号
    public String getLastLSH(String dateStr) throws Exception {
        /**
         * 本方法执行内容以及步骤
         *
         * @kuroneko1 创建Scoket链接
         * @kuroneko2 组装要发送的报文
         * @kuroneko3 发送报文，并获取返回的报文信息
         */

        log.info("DMZ->CSP，当前时间;{}", DateUtil.getDateTimeStr());
        /* @kuroneko1 */
        ConnectParam param = showSocketParam();
        log.info("socket参数：{}", param.toString());
        ISocketLog iSocketLog = new SocketLog();
        ISocketClient client = new SocketClientNew();
        /* @kuroneko2 */

        //获取请求报文流
        String message = dateStr;
        log.info("发送csp报文：{}", message);
        System.out.println("-----------------DMZ->CSP 传送指令-------------------------------------");
        /*发送报文到CSP并接收返回信息*/
        log.info("++++++++++++++++++++++++++++++++++++++++++++BEGIN++++++++++++++++++++++++++++++++++++++++");
        log.info("------------------------[DMZ->CSP]----------------------------------------");
        //报文：4位的字符串长度+交易码+请求报文内容
        String messageToCSP = new DecimalFormat("0000").format(message.getBytes("GBK").length + 4) + "yy11" + message;
        log.info(messageToCSP);

        //发送CSP，并接收CSP返回报文result1
        String result1 = client.send(messageToCSP, param, iSocketLog);
        log.info("------------------------[CSP->DMZ]--------------------------------------");

        //此处增加返回报文的判断逻辑，如果为空或者长度小于8，则不进行截取
        if (!"".equals(result1) && result1.length() > 8) {
            for (String s : Arrays.asList("CSP反馈的报文为：" + result1, "CSP上一次处理的流水号为：" + result1.split("\\|")[1], "CSP上个流水号对方接收的状态为：" + result1.split("\\|")[2], "CSP上个流水号给对方返回的报文为：" + result1.split("\\|")[3])) {
                log.info(s);
            }
            return result1;
        } else {
            log.error("CSP返回报文内容有错，长度小于8或者为空{}", result1);
        }

        return null;
    }

    //此交易为获取上一次需重发处理的流水号
    public String getLastResendLsh(String dateStr) throws Exception {
        /**
         * 本方法执行内容以及步骤
         *
         * @kuroneko1 创建Scoket链接
         * @kuroneko2 组装要发送的报文
         * @kuroneko3 发送报文，并获取返回的报文信息
         */

        System.out.println("DMZ->CSP，当前时间;" + DateUtil.getDateTimeStr());
        /* @kuroneko1 */
        ConnectParam param = showSocketParam();
        ISocketLog iSocketLog = new SocketLog();
        ISocketClient client = new SocketClientNew();
        /* @kuroneko2 */

        //获取请求报文流
        String message = dateStr;

        System.out.println("-----------------DMZ->CSP 传送指令-------------------------------------");
        /*发送报文到CSP并接收返回信息*/
        log.info("++++++++++++++++++++++++++++++++++++++++++++BEGIN++++++++++++++++++++++++++++++++++++++++");
        log.info("------------------------[DMZ->CSP]----------------------------------------");
        //报文：4位的字符串长度+交易码+请求报文内容
        String messageToCSP = new DecimalFormat("0000").format(message.getBytes("GBK").length + 4) + "yy15" + message;
        log.info(messageToCSP);

        //发送CSP，并接收CSP返回报文result1
        String result1 = client.send(messageToCSP, param, iSocketLog);
        log.info("------------------------[CSP->DMZ]--------------------------------------");

        //此处增加返回报文的判断逻辑，如果为空或者长度小于8，则不进行截取
        if (!"".equals(result1) && result1.length() > 8) {
            if (!result1.substring(4, 8).equals("9998")) {
                log.info("CSP反馈的报文为：{}", result1);
                for (String s : Arrays.asList("CSP上一次需重发处理的流水号为：" + result1.split("\\|")[1], "CSP上一次需重发处理的指令编码为：" + result1.split("\\|")[2], "CSP上个需重发流水号对方接收的状态为：" + result1.split("\\|")[3], "CSP上个需重发流水号给对方返回的报文为：" + result1.split("\\|")[4])) {
                    log.info(s);
                }
            }
            return result1;
        } else {
            log.error("CSP返回报文内容有错，长度小于8或者为空{}", result1);
        }

        return null;
    }

    //此交易为更新对方接收成功失败标识交易
    public String updateLSHStatus(String dateStr) throws Exception {
        /**
         * 本方法执行内容以及步骤
         *
         * @kuroneko1 创建Scoket链接
         * @kuroneko2 组装要发送的报文
         * @kuroneko3 发送报文，并获取返回的报文信息
         */

        System.out.println("DMZ->CSP，当前时间;" + DateUtil.getDateTimeStr());
        /* @kuroneko1 */
        ConnectParam param = showSocketParam();
        ISocketLog iSocketLog = new SocketLog();
        ISocketClient client = new SocketClientNew();
        /* @kuroneko2 */

        //获取请求报文流
        String message = dateStr;

        System.out.println("-----------------DMZ->CSP 传送指令-------------------------------------");
        /*发送报文到CSP并接收返回信息*/
        log.info("------------------------[DMZ->CSP]----------------------------------------");
        //报文：4位的字符串长度+交易码+请求报文内容
        String messageToCSP = new DecimalFormat("0000").format(message.getBytes("GBK").length + 4) + "yy13" + message;
        log.info(messageToCSP);

        //发送CSP，并接收CSP返回报文result1
        String result1 = client.send(messageToCSP, param, iSocketLog);
        log.info("------------------------[CSP->DMZ]--------------------------------------");

        //此处增加返回报文的判断逻辑，如果为空或者长度小于8，则不进行截取
        if (!"".equals(result1) && result1.length() >= 8) {
            log.info("CSP反馈的报文为：" + result1);

            return result1.substring(8).trim();
        } else {
            System.out.println("CSP返回报文内容有错，长度小于8或者为空" + result1);
            log.error("CSP返回报文内容有错，长度小于8或者为空");
        }

        return null;
    }

    //此交易为获取动账通知的流水号
    public String getHBResendLsh(String dateStr) throws Exception {
        /**
         * 本方法执行内容以及步骤
         *
         * @kuroneko1 创建Scoket链接
         * @kuroneko2 组装要发送的报文
         * @kuroneko3 发送报文，并获取返回的报文信息
         */

        log.info("DMZ->CSP，当前时间;{}", DateUtil.getDateTimeStr());
        /* @kuroneko1 */
        ConnectParam param = showSocketParam();
        ISocketLog iSocketLog = new SocketLog();
        ISocketClient client = new SocketClientNew();
        /* @kuroneko2 */

        //获取请求报文流
        String message = dateStr;

        log.info("-----------------DMZ->CSP 传送指令-------------------------------------");
        /*发送报文到CSP并接收返回信息*/
        log.info("++++++++++++++++++++++++++++++++++++++++++++BEGIN++++++++++++++++++++++++++++++++++++++++");
        log.info("------------------------[DMZ->CSP]----------------------------------------");
        //报文：4位的字符串长度+交易码+请求报文内容
        String messageToCSP = new DecimalFormat("0000").format(message.getBytes("GBK").length + 4) + "y111" + message;
        log.info(messageToCSP);

        //发送CSP，并接收CSP返回报文result1
        String result1 = client.send(messageToCSP, param, iSocketLog);
        log.info("------------------------[CSP->DMZ]--------------------------------------");

        //此处增加返回报文的判断逻辑，如果为空或者长度小于8，则不进行截取
        if (!"".equals(result1) && result1.length() > 8) {
            if (!"9998".equals(result1.substring(4, 8))) {
                for (String s : Arrays.asList("CSP反馈的报文为：" + result1, "CSP上一次需重发处理的流水号为：" + result1.split("\\|")[1], "CSP上一次需重发处理的指令编码为：" + result1.split("\\|")[2], "CSP上个需重发流水号对方接收的状态为：" + result1.split("\\|")[3], "CSP上个需重发流水号给对方返回的报文为：" + result1.split("\\|")[4])) {
                    log.info(s);
                }
            }
            return result1;
        } else {
            log.error("CSP返回报文内容有错，长度小于8或者为空{}", result1);
        }

        return null;
    }


    /**
     * 获取Socket配置
     *
     * @return
     */
    private ConnectParam showSocketParam() {
        ConnectParam param = new ConnectParam();
        param.setName("二手房交易");
        param.setIp("26.28.11.120");
        param.setPort(17007);
        param.setClientTryTimes(1);
        //log.info("socket ip 端口：{} : {}", sktServerIp, sktServerPort);
        return param;
    }

}
